/*!
 * 日期控件.
 * ecp.qzz.ui.input.datepicker.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.iedit","qzz.pcalendar"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.input");
	/**
	 * 日期控件.
	 */
	qzz.ui.input.datepicker = qzz.Class(qzz.ui.input.edit, {
		/**
		 * 创建.
		 */
	    _create:function() {
	    	this.base();
		    this.className = "DatePicker";
		    this.logInfo("date.create");
		    this.button = this.createElement("span");
		    this.showTime = this.getBooleanValue(this.getOption().showTime, false);
		    this.cyctype = this.getOption().cyctype || "day";
		    this.maxDate = "9999-12-31";
		    this.minDate = "1000-01-01";
		    
		    this.dateObj = new qzz.date();	        
            this.dateObj.setCycType(this.cyctype);
	
	    },
	    /**
	     * 渲染.
	     */
	    _render:function(){
	    	this.base();
	    	//日期面板控件
		    this.calendar = new qzz.ui.drop.popcalendar(undefined, {"cssPrefix":this._cssPrefix});
		    //初始化周期
		    this.calendar.setCycType(this.cyctype);
	    	this.logInfo("date.render");
	    	this.setStyle(this.button, "componet_ui_input_dateBotton");
	    	//最大日期，最小日期设置
	    	if(this.isNotEmpty(this.getOption().maxDate)) {
				this.setMaxDate(this.getOption().maxDate);
			}
			if(this.isNotEmpty(this.getOption().minDate)) {
				this.setMinDate(this.getOption().minDate);
			}
	    	//日期控件，默认为换行.
            if(this.getOption().wrap !== true) {
	    	    this.inputBox.style.whiteSpace = "nowrap";
	    	}
	    	var _this = this;
	    	//按钮事件
	    	this.button.onclick = function() {
	    		//禁用时直接退出.
	    		if(_this.getOption().disabled === true || _this.getOption().readOnly === true) return;
	    		_this.logInfo("click");
	    		var date = _this.getText();
	    		_this.calendar.setDate(date);
	    		_this.getAXY();
	    		_this.calendar.setCycType(_this.cyctype);
	    		_this.calendar.setShowTime(_this.showTime);
	    		_this.calendar.setMaxDate(_this.maxDate);
	    		_this.calendar.setMinDate(_this.minDate);
	    		var _left = _this.getALeft() + _this.getWidth() - _this.calendar.getWidth();
	    		if(_left < 0) {
	    			_left = 0;
	    		}
	    		//下拉面板
	    		_this.calendar.execute(_this.getATop() + _this.getHeight() - 4, 
	    				_left, _this.getHeight());
	    		_this._popState = true;
	    		if(typeof _this.onDrop == "function") {
	    			try {	    				
	    			    _this.onDrop(_this);
	    			} catch(e) {
	    				_this.logInfo("[onDrop]ERR:" + e.message);
	    			}
	    		}
	    	}	    	
	    	//选择事件
	    	this.calendar.onSelect = function(date) {
	    		var vl = date.getDate();
	    		var txt = _this._getTextByValue(vl);
	    		_this._checkRount(vl);
	    		var re = {};
	    		re[_this.getOption().idkey] = vl;
	    		re[_this.getOption().capkey] = txt;
	            _this.setValue(re);
	            if(typeof _this.onSelect == "function") {
	            	try {
	            	    _this.onSelect(vl);
	            	} catch(e) {
	            		_this.logInfo("[onSelect]ERR:" + e.message);
	            	}	            	    
	            }
	            _this.trigger("onSelect", [vl]);
	        }
	    	/**
	    	 * 隐藏事件.
	    	 */
	    	this.calendar.onHide = function() {
	    		_this._popState = false;
	            if(typeof _this.onHide == "function") {
	            	try {	            		
	            	    _this.onHide();
	            	} catch(e) {
	            		_this.logInfo("[onHide]ERR:" + e.message);
	            	}	            	    
	            }
	        }
	    	this.calendar.setParent(this.getModelBody());	    	
	    },
	    /**
	     * 检查日期是否超出最大最小值.
	     */
	    _checkRount:function(date) {
	    	this.calendar.setMaxDate(this.maxDate);
    		this.calendar.setMinDate(this.minDate);
    		var cr = this.calendar.checkRound(date);
    		if(cr.state === "max") {
    			this.warnInfo = this.getI18n("qzz.dateMaxer", "当前日期，大于最大日期") + this.maxDate;
    		} else if(cr.state === "min") {
    			this.warnInfo = this.getI18n("qzz.dateMiner", "当前日期，小于最小日期") + this.minDate;
    		} else {
    			this.warnInfo = "";
    		}
    		return cr.date;
	    },
		/**
	     * 通过val获取text值.
	     * @param {string|number} val 值
	     * @return {string} text 显示值
	     * @ignore
	     */
	    _getTextByValue:function(val) {	
	    	return this.dateObj.getFormatDate(val);
	    },
	    /**
	     * 提交值.
	     */
	    postValue:function() {
	    	if(this.calendar.isShow()) {
	    		return false;
	    	}
	    	var text = this.inputBox.value;
	    	if(typeof text == "object") {
	    		text = text[this.getOption().capkey];
	    		if(this.trim(this.inputBox.value) !== "" && text != this.trim(this.inputBox.value)) {
	    			text = this.inputBox.value;
	    		}
	    	}
	    	if(text === "" && this.notnull === true) return;
	    	if(text !== "") {
	    		var vl = this.calendar.checkDate(text);
	    		this._checkRount(vl);
	    		if(vl !== "") {
	    			var txt = this._getTextByValue(vl);
	    			var re={};
	    			re[this.getOption().idkey] = vl;
	    			re[this.getOption().capkey] = txt;
	    	        this.setValue(re);
	    		}
	    	} else {
	    		this.setValue(text);
	    	}
	    },
	    /**
	     * 失去焦点事件.
	     */
	    doBlur:function() {
	    	this.base();    	
	    	if(this.calendar != null && !this.calendar.isFocus()) {
	    		this.calendar.setBlur();
	    	}
	    },
	    /**
	     * 显示时间.
	     * @param value 状态
	     */
	    setShowTime:function(value) {
	    	this.showTime = this.getBooleanValue(value, false);
	    },
	    /**
	     * 键盘按下.
	     * @param ev 事件对象
	     * @param key 键值
	     */
	    doKeyDown:function(ev, key) {
	    	this.base(ev, key);
	    	if(key === this.keyBoard.F2) {
	    		this.button.onclick();
	    	}
	    },
	    /**
	     * 日期周期.
	     */
	    setCycType:function(value) {
	    	this.cyctype = value;
	    },
	    
	    refreash:function() {
	    	this.refresh();
	    },
	    /**
	     * 刷新.
	     */
	    refresh:function(){
	    	this.base();
	    	var w = (this.getWidth() - 20);
	    	if(w < 1) {
	    		w = 1;
	    	}
	    	this.inputBox.style.width = w + "px";
	    	this.button.style.height = (this.getHeight() - 2) + "px";
	    	this.button.style.backgroundPosition = "left center";//((this.getHeight() - 4)/2) + "px";
	    	this.thisWindow.appendChild(this.button);
	    },
	    /**
	     * 设置最大日期.
	     * @param date 日期
	     */
	    setMaxDate:function(date) {
	    	this.maxDate = date;
	    	
	    },
	    /**
	     * 设置最水日期.
	     * @param date 日期
	     */
	    setMinDate:function(date){
	    	this.minDate = date;	    	
	    },
	    /**
	     * 
	     */
	    getPopState:function() {
	    	return this.calendar.isShow();
	    }
	});
	
	/**
	 * 文本控件.
	 */
	_bindRenderFunc("qzzdatepicker", qzz.ui.input.datepicker);
	return qzz.ui.input.datepicker;
});